# Copyright 2011-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Tests of ambiguous linespecs.

standard_testfile lspec.cc

set exefile $testfile

set baseone base/one/thefile.cc
set basetwo base/two/thefile.cc

if {[skip_cplus_tests]} {
    unsupported linespec.exp
    return
}

if {[prepare_for_testing ${testfile}.exp $exefile \
	 [list $srcfile $baseone $basetwo] \
	 {debug nowarnings c++}]} {
    return -1
}

gdb_test_no_output "set multiple-symbols all" \
    "set multiple-symbols to all for linespec tests"

set l1 [gdb_get_line_number "thefile breakpoint" $baseone]
set l2 [gdb_get_line_number "thefile breakpoint" $basetwo]

if {$l1 != $l2} {
    error "somebody incompatibly modified the source files needed by linespec.exp"
}

# Copying files to a remote host loses the directory prefix during
# compilation.
if { [is_remote host] } {
    untested "breakpoints using dir/file:line"
} else {
    gdb_test "break one/thefile.cc:$l1" \
        "Breakpoint $decimal at $hex: file .*thefile.cc, line $l1." \
        "single-location break using dir/file:line"

    gdb_test "clear one/thefile.cc:$l1" \
        "Deleted breakpoint $decimal *" \
        "clear breakpoint using dir/file:line"
}

gdb_test "break thefile.cc:$l1" \
    "Breakpoint $decimal at $hex: thefile.cc:$l1. \[(\]2 locations\[)\]" \
    "multi-location break using file:line"

gdb_test "break dupname" \
    "Breakpoint $decimal at $hex: dupname. \[(\]2 locations\[)\]" \
    "multi-location break using duplicate function name"

gdb_test "break dupname:label" \
    "Breakpoint $decimal at $hex: dupname:label. \[(\]2 locations\[)\]" \
    "multi-location break using duplicate function name and label"

# Testing if the "condition" command completes only the breakpoints,
# not the locations.
gdb_test "complete condition " "condition $decimal\r\ncondition $decimal\r\ncondition $decimal"

gdb_test_no_output "set breakpoint pending off" \
    "disable pending breakpoints for linespec tests"

# This is PR breakpoints/12856.
gdb_test "break lspec.cc:nosuchfunction" \
    "Function \"nosuchfunction\" not defined in \"lspec.cc\"." \
    "set breakpoint on non-existent function"

gdb_test "break NameSpace::overload" \
    "Breakpoint \[0-9\]+ at $hex: NameSpace::overload. \[(\]3 locations\[)\]" \
    "set breakpoint at all instances of NameSpace::overload"

gdb_test "break lspec.cc:NameSpace::overload" \
    "Breakpoint \[0-9\]+ at $hex: file .*lspec.cc, line 7." \
    "set breakpoint at lspec.cc instance of NameSpace::overload"

gdb_test "break lspec.cc:NameSpace::overload(double)" \
    "Function \"NameSpace::overload\\(double\\)\" not defined in \"lspec.cc\"." \
    "set breakpoint at non-existent lspec.cc instance of NameSpace::overload"

gdb_test "break NameSpace::overload()" \
    "Breakpoint \[0-9\]+ at $hex: file .*lspec.cc, line 7." \
    "set breakpoint at specific instance of NameSpace::overload"

# This should manage to set a breakpoint even though body.h does not
# include all of the function in question.
set line [gdb_get_line_number "body breakpoint no code" body.h]
gdb_test "break body.h:$line" \
    "Breakpoint \[0-9\]+.*" \
    "set breakpoint in body.h"

# This should only have a single location -- in f1.
set line [gdb_get_line_number "f1 breakpoint" lspec.h]
gdb_test "break lspec.h:$line" \
    "Breakpoint \[0-9\]+ at $hex: file .*lspec.h, line $line." \
    "set breakpoint in f1"

#
# Multi-inferior tests.
#

gdb_test "add-inferior" "Added inferior 2" \
    "add inferior for linespec tests"

gdb_test "inferior 2" "Switching to inferior 2 .*" \
    "switch to inferior 2 for linespec tests"

# Note that in particular this should not cause errors when re-setting
# breakpoints.
gdb_test "file $binfile" \
    "Reading symbols from .*done." \
    "set the new inferior file for linespec tests"

gdb_test "break main" \
    "Breakpoint \[0-9\]+ at $hex: main. .2 locations." \
    "set breakpoint at main in both inferiors"
